const app = getApp()
var api = require('../../utils/api.js');
Page({
  data: {
    motto: 'Hello World 秃头鸭',
    userInfo: {},
    modalName: null,
    posterimg: null,
    hasUserInfo: false,
    yearBtn:1,
    img:'',
    img1:'',
    img3:'',
    img5:'',
    jobNum:'',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //切换年份
  changeYear(e){
    var that = this;
    var year = e.currentTarget.dataset.year;
    that.setData({
      yearBtn: year
    })
    if(year=='1'){
      that.setData({
        img:that.data.img1,
        posterimg:null
      })
    }
    if(year=='3'){
      that.setData({
        img:that.data.img3,
        posterimg:null
      })
    }
    if(year=='5'){
      that.setData({
        img:that.data.img5,
        posterimg:null
      })
    }

  },
  //返回主页
  toHome() {
    wx.navigateTo({
      url: '/pages/bald/bald',
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    this.setData({
      img: 'data:image/jpg;base64,' + decodeURIComponent(options.data0),
      img1: 'data:image/jpg;base64,' + decodeURIComponent(options.data0),
      img3: 'data:image/jpg;base64,' + decodeURIComponent(options.data1),
      img5: 'data:image/jpg;base64,' + decodeURIComponent(options.data2),
      jobNum:options.jobNum
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  //获取用户信息
  getUserInfo: function (e) {
    var that = this;
    if (e.detail.errMsg =='getUserInfo:ok'){
      app.globalData.userInfo = e.detail.userInfo
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      //生成海报
      that.posterGenerate();
    }else{
      wx.showToast({
        icon:'none',
        title: '未授权无法生成海报',
        mask: true
      })
    }
  },
  //生成海报
  posterGenerate: function () {
    var that = this;
    wx.showLoading({
      title: "生成海报中...",
      mask: true
    })
    if (that.data.posterimg==null){
      api.posterRequestByBase64(that.data.img.slice(22), that.data.userInfo.avatarUrl,encodeURIComponent(that.data.userInfo.nickName) ,that.data.jobNum, {
        success(result) {
          wx.hideLoading();
          if (result.code == 200) {
            that.setData({
              posterimg: 'data:image/jpg;base64,' + result.data.image,
              modalName: 'poster'
            })
          } else {
            wx.hideLoading();
            that.setData({
              modalName: null,
            })
            wx.showModal({
              content: result.msg_zh,
              showCancel: false,
              confirmText: '好的'
            })
          }
        }
      })
    }else{
      wx.hideLoading();
      that.setData({
        modalName: 'poster'
      })
    }
  },
  //关闭窗口
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //关闭窗口并保存图片
  hideModalSave(e) {
    var that = this;
    var files = wx.getFileSystemManager();
    wx.showLoading({
      title: "保存图片中...",
      mask: true
    })
    files.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/bald.jpg',
      data: that.data.posterimg.slice(22),
      encoding: 'base64',
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/bald.jpg',
          success: function (res) {
            console.info(res);
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              mask: true
            })
            that.hidePoster();
          },
          fail: function (res) {
            if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied" 
              || res.errMsg === "saveImageToPhotosAlbum:fail auth deny" 
              || res.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
            
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次保存海报分享即可保存哦~',
                          showCancel: false,
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册哦~',
                          showCancel: false,
                        })
                      }
                    },
                    fail(failData) {
                
                    },
                    complete(finishData) {
          
                    }
                  })
                }
              })
            }
            wx.hideLoading();
            that.hidePoster();
          }
        })
      },
      fail: function (res) {
        that.hidePoster();
        wx.showToast({
          icon: 'none',
          title: '保存失败',
          mask: true
        })
      }
    })
  },
  //隐藏海报
  hidePoster() {
    var that = this;
    that.setData({
      modalName: null
    })
  }
})
